// Define variables
sbiz.CleanSubject.subject = null;
sbiz.CleanSubject.reply_prefix_single = "Re:";
sbiz.CleanSubject.reply_prefix = sbiz.CleanSubject.defaultReplyPrefix;
sbiz.CleanSubject.lastMatchedPrefix = null;
sbiz.CleanSubject.lastMatchedPrefixCount = 0;
sbiz.CleanSubject.lastMatchedPrefixMaxCount = 20;
sbiz.CleanSubject.subjectElementID = "msgSubject";
sbiz.CleanSubject.subjectResultElementID = "msgSubject";  

// Initialize the component and set the variables
sbiz.CleanSubject.Init = function ()
{    
    sbiz.CleanSubject.subject = document.getElementById(sbiz.CleanSubject.subjectElementID).value;
    sbiz.CleanSubject.prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);

    // Set prefix array
    sbiz.CleanSubject.SetPrefixArrayFromPreferences();
    
    // Set other variables
    sbiz.CleanSubject.SetVariablesFromPreferences();
    
    // Set the Re: itself to the list of prefixes that will be deleted
    if (!(sbiz.CleanSubject.ArrayContains(sbiz.CleanSubject.prefixes, sbiz.CleanSubject.reply_prefix_single)))
    {
        sbiz.CleanSubject.prefixes[sbiz.CleanSubject.prefixes.length] = sbiz.CleanSubject.reply_prefix_single;
    }
    sbiz.CleanSubject.Clean();
};

// Clean the subject from the fancy prefixes that the user has specified
sbiz.CleanSubject.Clean = function()
{
    // Subject matches the user-specified settings?
    if (sbiz.CleanSubject.IsReply() || sbiz.CleanSubject.IsForwarded())
    {
        // Slice the subject to not include the pre and/or postfixes inserted by Thunderbird
        var realSubject = sbiz.CleanSubject.GetSlicedSubject(); 

        realSubject = sbiz.CleanSubject.RemovePrefixes( realSubject, sbiz.CleanSubject.prefixes );
                
        // set the new subject
        document.getElementById(sbiz.CleanSubject.subjectResultElementID).value = sbiz.CleanSubject.GetFinalSubject(realSubject);
    }
};

// Checks if the subject seems to be a reply
sbiz.CleanSubject.IsReply = function()
{
    // Look if Re: is at the beginning
    return ((sbiz.CleanSubject.subject.toLowerCase()).indexOf(sbiz.CleanSubject.reply_prefix.toLowerCase()) == 0);
}

// Checks if the subject seems to be a forwarded mail
sbiz.CleanSubject.IsForwarded = function()
{
    // Look if [Fwd: is at the beginning and if the user has forwarding enabled
    return ((sbiz.CleanSubject.forward == true) &&
        ((sbiz.CleanSubject.subject.toLowerCase()).indexOf(sbiz.CleanSubject.defaultForwardPrefix.toLowerCase()) == 0));
}

// Slice the subject to not include the pre and/or postfixes inserted by Thunderbird
sbiz.CleanSubject.GetSlicedSubject = function()
{
    var result;
    if (sbiz.CleanSubject.IsReply())
    {
        result = (sbiz.CleanSubject.subject).slice((sbiz.CleanSubject.reply_prefix).length);
    }
    else if (sbiz.CleanSubject.IsForwarded())
    {
        result = (sbiz.CleanSubject.subject).slice((sbiz.CleanSubject.defaultForwardPrefix).length);
        // Clean from ending ]
        if (result.substr(result.length - 1) == sbiz.CleanSubject.defaultForwardPostfix)
        {
             result = result.slice(0, result.length - 1);
        }
    }
    else
    {
        // Should not happen
        result = sbiz.CleanSubject.subject;
    }
    
    return result;
}

// Creates the final subject with pre and postfixes depending on reply or forward
sbiz.CleanSubject.GetFinalSubject = function(subject)
{
    var result = sbiz.CleanSubject.Trim(subject);
    if (sbiz.CleanSubject.IsReply())
    {
        result = sbiz.CleanSubject.replyPrefix + result + sbiz.CleanSubject.replyPostfix;
    }
    else if (sbiz.CleanSubject.IsForwarded())
    {
        result = sbiz.CleanSubject.forwardPrefix + result + sbiz.CleanSubject.forwardPostfix;
    }
    else
    {
        // Should not happen
        result = sbiz.CleanSubject.subject;
    }
    
    return result;
}

// Call the init method when the composer gets opened
window.addEventListener(
    "compose-window-init", 
    sbiz.CleanSubject.Init, 
    true
);
window.addEventListener(    
    "compose-window-reopen", 
    sbiz.CleanSubject.Init, 
    true
);